package tij4.concurrent.exercise.ex27;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

class Meal {
    private final int orderNum;

    public Meal(int orderNum) {
        this.orderNum = orderNum;
    }

    @Override
    public String toString() {
        return "Meal" + orderNum;
    }
}

class WaitPersion implements Runnable {
    private Restaurant restaurant;
    protected ReentrantLock reentrantLock = new ReentrantLock();
    protected Condition condition = reentrantLock.newCondition();

    public WaitPersion(Restaurant restaurant) {
        this.restaurant = restaurant;
    }

    @Override
    public void run() {
        try{
            while (!Thread.interrupted()) {
//                synchronized (this) {
                reentrantLock.lock();
                try {
                    while (restaurant.meal == null) {
                        condition.await();
                    }
                }finally {
                    reentrantLock.unlock();
                }
                System.out.println("Waitpersion got " + restaurant.meal);
//                synchronized (restaurant.chef) {
                restaurant.chef.reentrantLock.lock();
                try {
                    restaurant.meal = null;
                    restaurant.chef.condition.signalAll();
                }finally {
                    restaurant.chef.reentrantLock.unlock();
                }
            }

        }catch (InterruptedException e) {
            System.out.println("WaitPersion interrupted");
        }
    }
}

class Chef implements Runnable {
    private Restaurant restaurant;
    private int count = 0;
    protected ReentrantLock reentrantLock = new ReentrantLock();
    protected Condition condition = reentrantLock.newCondition();

    public Chef(Restaurant restaurant) {
        this.restaurant = restaurant;
    }

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
//                synchronized (this) {
                reentrantLock.lock();
                try {
                    while (restaurant.meal != null)
                        condition.await();
                }finally {
                    reentrantLock.unlock();
                }
                if (++count == 10) {
                    System.out.println("Out of food, closing");
                    restaurant.exec.shutdownNow();
                }
                System.out.println("Order up!");
//                synchronized (restaurant.waitPersion) {
                restaurant.waitPersion.reentrantLock.lock();
                try {
                    restaurant.meal = new Meal(count);
                    restaurant.waitPersion.condition.signalAll();
                }finally {
                    restaurant.waitPersion.reentrantLock.unlock();
                }
                TimeUnit.MILLISECONDS.sleep(100);
            }
        }catch (InterruptedException e) {
            System.out.println("Chef interrrupted");
        }
    }
}

public class Restaurant {
    Meal meal;
    ExecutorService exec = Executors.newCachedThreadPool();
    WaitPersion waitPersion = new WaitPersion(this);
    Chef chef = new Chef(this);

    public Restaurant() {
        exec.execute(chef);
        exec.execute(waitPersion);
    }

    public static void main(String[] args) {
        new Restaurant();
    }
}
